package com.powernode.shop.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/12
 */
@EnableCaching      // 注解开发redis
@Configuration
public class RedisCacheConfig {
    /**
     * 配置redis缓存，json串方式存入redis，以便于注解开发
     * @return
     */
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(){
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.
                fromSerializer(RedisSerializer.json()))
                .entryTtl(Duration.ofMinutes(5));       // 配置过期时间
        return redisCacheConfiguration;
    }
}
